import sys


import pymysql

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class insert_window(QDialog):
    Signal_TwoParameter = pyqtSignal(str)

    def __init__(self, parent=None):
        super(insert_window, self).__init__(parent)
        self.setWindowTitle('插入数据')
        # 在布局中添加部件
        layout = QVBoxLayout(self)
        self.resize(640, 200)

        self.label = QLabel(self)
        self.label.setText('请输入需要插入的数据')

        self.tablewidget1 = QTableWidget()
        self.tablewidget1.setRowCount(1)
        self.tablewidget1.setColumnCount(6)
        # 设置列名
        self.tablewidget1.setHorizontalHeaderLabels(["车牌号", "时间", "姓名", "手机号", "是否处理", "图片"])


        # 使用两个button(ok和cancel)分别连接accept()和reject()槽函数
        buttons = QDialogButtonBox(
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
            Qt.Horizontal, self)
        buttons.accepted.connect(self.insert)
        buttons.rejected.connect(self.reject)


        button_clean = QPushButton("清空数据")
        button_clean.clicked.connect(self.clean)

        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(button_clean)
        hbox.addStretch(1)

        layout.addWidget(self.label)
        layout.addWidget(self.tablewidget1)
        layout.addLayout(hbox)
        layout.addWidget(buttons)


    # def insert(self):
    #     # t = threading.Thread(target=self.insert_handle)
    #     # t.start()  # 告诉操作系统开一个线程
    def insert(self):
        # 获取表格内容
        id = self.tablewidget1.item(0, 0)
        time = self.tablewidget1.item(0, 1)
        name = self.tablewidget1.item(0, 2)
        phone = self.tablewidget1.item(0, 3)
        deal = self.tablewidget1.item(0, 4)
        image = self.tablewidget1.item(0, 5)

        if id == None or time == None:
            QMessageBox.critical(self, '错误', '车牌号与时间为必填项！', QMessageBox.Yes)
            return
        else:
            id = id.text()
            time = time.text()
            if id == '' or time == '':
                QMessageBox.critical(self, '错误', '车牌号与时间为必填项！', QMessageBox.Yes)
                return

        list = [name, phone, deal, image]
        list_transition = []

        for i in list:
            if i == None:
                i = "暂无"
            else:
                i = i.text()
                if i == '':
                    i = "暂无"
                i = i.replace('\\', '/')
            list_transition.append(i)

        name = list_transition[0]
        phone = list_transition[1]
        deal = list_transition[2]
        image = list_transition[3]


        conn = pymysql.connect(host='localhost', port=3306, user='root', password="root", db="tab")
        cur = conn.cursor()

        # 插入的sql语句
        try:
            sql1 = "INSERT INTO tab (Mid, Mdate, Mname, Mphone, Mdeal, Mimage) VALUES " \
                   "('" + id + "','" + time + "','" + name + "','" + phone + "','" + deal + "','" + image + "')"
            print(sql1)
            cur.execute(sql1)
            # 事务提交数据库
            conn.commit()
            cur.close()
            conn.close()

            self.Signal_TwoParameter.emit('insert_window_succeed')
            print("插入成功")
        except Exception as e:
            print(e)
            print("插入失败")
            QMessageBox.critical(self, '错误', '数据库中有该数据，如需修改，请修改按纽', QMessageBox.Yes)
            self.Signal_TwoParameter.emit('insert_window_fail')

    def clean(self):
        self.tablewidget1.clearContents()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = insert_window()
    main.show()
    sys.exit(app.exec_())
